package com.example.academicsystem.server.service.impl;

import com.example.academicsystem.constant.MessageConstant;
import com.example.academicsystem.exception.DeleteException;
import com.example.academicsystem.exception.InsertException;
import com.example.academicsystem.exception.QueryFailedException;
import com.example.academicsystem.exception.UpdateException;
import com.example.academicsystem.pojo.dto.*;
import com.example.academicsystem.pojo.vo.CourseVo;
import com.example.academicsystem.pojo.vo.GradeVo;
import com.example.academicsystem.result.PageResult;
import com.example.academicsystem.server.dao.TeacherMapper;
import com.example.academicsystem.server.service.TeacherService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 分页查询课程
     * */
    public PageResult studentCoursePageQuery(CourseDto courseDto) {
        if (courseDto.getId() == null || courseDto.getId() == "")
            throw new QueryFailedException(MessageConstant.PARAM_NULL);
        PageHelper.startPage(courseDto.getPage(), courseDto.getPageSize());
        List<String> courseIdList = teacherMapper.teacherCourseSelectAll(courseDto.getId());
        if (courseIdList.size() == 0) return null;
        Page<CourseVo> page = teacherMapper.coursePageQuery(courseIdList, courseDto.getCourseName());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除选课
     * */
    public void teacherCourseDelete(TeacherCourseDto teacherCourseDto) {
        if (teacherCourseDto.getCourseId()==null ||teacherCourseDto.getTeacherId()==null)
            throw new DeleteException(MessageConstant.PARAM_NULL);
        teacherMapper.teacherCourseDelete(teacherCourseDto);
    }

    /**
     * 新增选课
     * */
    public void teacherCourseInsert(TeacherCourseDto teacherCourseDto) {
        if (teacherCourseDto.getTeacherId()==null ||teacherCourseDto.getCourseId()==null)
            throw new InsertException(MessageConstant.PARAM_NULL);
        teacherMapper.teacherCourseInsert(teacherCourseDto);
    }

    /**
     * 分页查询成绩
     * */
    public PageResult teacherGradePageQuery(GradeDto gradeDto) {
        if (gradeDto.getTeacherId()==null || gradeDto.getTeacherId()=="")
            throw new QueryFailedException(MessageConstant.PARAM_NULL);
        PageHelper.startPage(gradeDto.getPage(),gradeDto.getPageSize());
        Page<GradeVo> page  = teacherMapper.gradePageQuery(gradeDto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增成绩
     * */
    public void teacherGradeInsert(GradeInsertDto gradeInsertDto) {
        if (gradeInsertDto.getTeacherId()==null ||gradeInsertDto.getTeacherId()=="")
            throw new InsertException(MessageConstant.PARAM_NULL);
        teacherMapper.teacherGradeInsert(gradeInsertDto);
    }

    /**
     * 修改成绩
     * */
    public void teacherGradeUpdate(TeacherGradeUpdateDto teacherGradeUpdateDto) {
        if (teacherGradeUpdateDto.getTeacherId()==null||teacherGradeUpdateDto.getCourseId()==null||teacherGradeUpdateDto.getStudentId()==null||teacherGradeUpdateDto.getTeacherId()=="")
            throw new UpdateException(MessageConstant.PARAM_NULL);
        teacherMapper.teacherGradeUpdate(teacherGradeUpdateDto);
    }
}